package com.coocaa.ops.admin.base.service;


import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.common.core.base.entity.BaseEntity;
import com.coocaa.ops.common.core.base.entity.BaseHistoryTableEntity;
import com.coocaa.ops.common.core.base.entity.BaseOriginTableEntity;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.base.util.BeanCopyUtil;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.base.util.VersionCodeUtil;

import java.util.Date;

/**
 * @author bijiahao
 * @date : 2018/9/8.
 * @description: 抽象保存业务实现-适用于带副本的-标准表结构-实体类
 */
public abstract class AbstractSaveServiceImpl<T extends BaseOriginTableEntity> extends BaseSaveServiceImpl<T> {

    /**
     * 获取基础业务接口实现对象
     *
     * @return
     */
    abstract public AbstractBizImpl getAbstractBizImpl();


    @Override
    public void handleInitHistoryVersionData(Integer autoIncrementId, Integer userId) {
        BaseHistoryTableEntity historyEntity = getAbstractBizImpl().queryHistoryByIdAndVersion(autoIncrementId, BaseAuditTaskProviderImpl.V_1);
        if (null == historyEntity) {
            historyEntity = getAbstractBizImpl().createHistoryEntityNewInstance();
            T entity = (T) getAbstractBizImpl().queryById(autoIncrementId);
            BeanCopyUtil.copyOrig2Target(entity, historyEntity, "原始表实体类复制->历史表实体类");
            historyEntity.setModifier(userId);
            getAbstractBizImpl().addHistory(historyEntity);
        }
    }

    /**
     * 查询当前数据库数据信息-根据自增ID
     *
     * @param autoIncrementId
     * @return
     */
    @Override
    public T queryCurrentEntityInDbById(Integer autoIncrementId) {
        return (T) getAbstractBizImpl().queryById(autoIncrementId);
    }


    /**
     * 处理-副本编辑
     *
     * @param inputEntity
     * @param userId
     */
    @Override
    public void handleCopyEdit(T inputEntity, Integer userId) {
        if (getAbstractBizImpl().isCopyHasDel(inputEntity.getId(), inputEntity.getCurrentVersion())) {
            throw new AdminBizException("The current copy has been deleted!");
        }
        BaseHistoryTableEntity historyEntityDb = getAbstractBizImpl().queryHistoryByIdAndVersion(inputEntity.getId(), inputEntity.getCurrentVersion());
        if (null != historyEntityDb && ResourceStatusValidator.isOnline(historyEntityDb.getStatus())) {
            throw new AdminBizException("Copies have been reviewed and are not editable!");
        }
        BaseHistoryTableEntity historyEntity = BeanCopyUtil.copyOrigin2History(inputEntity, userId, getAbstractBizImpl().createHistoryEntityNewInstance());
        handleDate(historyEntity);
        getAbstractBizImpl().updateHistory(historyEntity, inputEntity.getId(), inputEntity.getCurrentVersion());
        //更新副本标识
        getAbstractBizImpl().updateDuplicateVersion(inputEntity.getId(), inputEntity.getCurrentVersion(), userId);
    }

    /**
     * 处理-当前编辑
     *
     * @param inputEntity
     * @param userId
     */
    @Override
    public void handleCurrentEdit(T inputEntity, Integer userId) {
        inputEntity.setModifier(userId);
        handleDate(inputEntity);
        getAbstractBizImpl().updateById(inputEntity);
    }

    /**
     * 处理-创建副本
     *
     * @param inputEntity
     * @param userId
     */
    @Override
    public void handleCreateCopy(T inputEntity, Integer userId) {
        if (getAbstractBizImpl().isExistCopy(inputEntity.getId())) {
            throw new AdminBizException("The replica has been created!");
        }
        BaseHistoryTableEntity historyEntity = BeanCopyUtil.copyOrigin2History(inputEntity, userId, getAbstractBizImpl().createHistoryEntityNewInstance());
        Integer maxVersion = getAbstractBizImpl().getHistoryMapper().queryMaxVersion(inputEntity.getId());
        historyEntity.setCurrentVersion(VersionCodeUtil.produceCode(maxVersion));
        handleDate(historyEntity);
        getAbstractBizImpl().addHistory(historyEntity);
        //保存历史表后给当前保存的对象赋版本信息
        inputEntity.setCurrentVersion(historyEntity.getCurrentVersion());
        //更新原始表中的副本版本信息
        getAbstractBizImpl().updateDuplicateVersion(inputEntity.getId(), historyEntity.getCurrentVersion(), userId);
    }

    /**
     * 处理-新增
     *
     * @param inputEntity
     * @param userId
     */
    @Override
    public void handleNewAdd(T inputEntity, Integer userId) {
        if (ResourceStatusValidator.isOffline(inputEntity.getStatus())) {
            inputEntity.setStatus(ResourceStatusEnums.ON_LINE.getStatus());
        }
        inputEntity.setCreator(userId);
        inputEntity.setModifier(userId);
        inputEntity.setCurrentVersion(VersionCodeUtil.produceCode(null));
        getAbstractBizImpl().add(inputEntity);
    }

    /**
     * 处理时间，防止前端意外传入使时间不正常
     *
     * @param entity
     */
    private void handleDate(BaseEntity entity) {
        entity.setCreatedDate(null);
        entity.setLastUpdateDate(null);
    }
}
